package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodDao foodDao;

    @Override
    public void add(ArrayList<Food> foods) {

        if (foods != null) {
            for (Food food : foods) {
                long count = foodDao.findFoodName(food.getId());
                if (count > 0) {
                    foodDao.update(food);
                } else {
                    foodDao.add(food);
                }
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        System.out.println(queryPageBean.getPageSize()+queryPageBean.getCurrentPage());
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Food> page = foodDao.findBySeach(queryPageBean.getQueryString());
        long total = page.getTotal();
        List<Food> list = page.getResult();
        PageResult pageResult = new PageResult(total, list);
        return pageResult;

    }

    @Override
    public void addFood(Food food) {
        foodDao.add(food);
    }


    @Override
    public List<Food> findAll() {
        return foodDao.findAll();
    }
}
